package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.common.utils.JwtUtil;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.app.mapper.GraphInfoMapper;
import com.atguigu.lease.web.app.mapper.LeaseAgreementMapper;
import com.atguigu.lease.web.app.service.*;
import com.atguigu.lease.web.app.vo.agreement.AgreementDetailVo;
import com.atguigu.lease.web.app.vo.agreement.AgreementItemVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {
    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;
    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private RoomInfoService roomInfoService;
    @Autowired
    private PaymentTypeService paymentTypeService;
    @Autowired
    private LeaseTermService leaseTermService;

    @Override
    public List<AgreementItemVo> getAgreeById(String token) {

        String phone = JwtUtil.parseToken(token).get("username", String.class);//登录接口中给username存放的是phone的值

        List<AgreementItemVo> agreementItemVos=leaseAgreementMapper.getAgreeByPhone(phone);
        for (AgreementItemVo itemVo : agreementItemVos) {
            itemVo.setRoomGraphVoList(graphInfoMapper.graphListByItem(itemVo.getRoomId(),ItemType.ROOM));
//            itemVo.setIsRenewable();  //todo
            //是否可续约  既有status=2【已签约】 又没有status=7【续约待确认】的数据才为true
            itemVo.setIsRenewable(leaseAgreementMapper.getIsRenewableStatus(itemVo.getRoomId()));
        }


        return agreementItemVos;
    }

    @Override
    public AgreementDetailVo getAgreeDetailById(Long id) {

        //租约
        LeaseAgreement leaseAgreement = leaseAgreementMapper.selectById(id);
        if (leaseAgreement == null) {
            return null;
        }

        Long apartId=leaseAgreement.getApartmentId();
        Long roomId=leaseAgreement.getRoomId();
        //公寓
        ApartmentInfo apartmentInfo = apartmentInfoService.getById(apartId);
        //公寓图片
        List<GraphVo> apartmentGraphVoList = graphInfoMapper.graphListByItem(apartId,ItemType.APARTMENT);
        //房间号
        RoomInfo roomInfo = roomInfoService.getById(roomId);
        //房间图片列表
        List<GraphVo> roomGraphVoList = graphInfoMapper.graphListByItem(roomId,ItemType.ROOM);
        //支付方式
        PaymentType paymentType = paymentTypeService.getById(leaseAgreement.getPaymentTypeId());
        //租期月数,租期单位
        LeaseTerm leaseTerm = leaseTermService.getById(leaseAgreement.getLeaseTermId());


        AgreementDetailVo agreementDetailVo = new AgreementDetailVo();
        BeanUtils.copyProperties(leaseAgreement, agreementDetailVo);
        agreementDetailVo.setApartmentName(apartmentInfo.getName());
        agreementDetailVo.setRoomNumber(roomInfo.getRoomNumber());
        agreementDetailVo.setApartmentGraphVoList(apartmentGraphVoList);
        agreementDetailVo.setRoomGraphVoList(roomGraphVoList);
        agreementDetailVo.setPaymentTypeName(paymentType.getName());
        agreementDetailVo.setLeaseTermMonthCount(leaseTerm.getMonthCount());
        agreementDetailVo.setLeaseTermUnit(leaseTerm.getUnit());

        return agreementDetailVo;
    }

}




